import type { DetailsResponse, ListResponse, ListResponseWithPagination, Response } from '@/http';
import { request } from '@/http';
import type { IAvailableCoupon } from './interfaces/coupons';
import type { IOrder, IOrderListItem } from './interfaces/orders';
import type { IPayment } from './interfaces/payments';
import type { IPreOrder } from './interfaces/pre-orders';

/**
 * 预约单下单
 */
export function createPreOrder(payload: {
  activityId: number;
  restaurantId: number;
  goodsId: number;
  bookingDate: string;
  bookingTime: string;
  bookingNum: number;
}): Promise<Response<{ preOrderSn: string }>> {
  return request.post<Response<{ preOrderSn: string }>>('/food/order/createPreOrder', payload);
}

/**
 * 获取预约单详情
 */
export function getPreOrder(preOrderSn: string): Promise<Response<IPreOrder>> {
  return request.post<Response<IPreOrder>>('/food/order/getPreOrderDetail', { preOrderSn });
}

/**
 * 订单可用优惠券列表
 */
export function getAvailableCoupons(preOrderSn: string): Promise<ListResponse<IAvailableCoupon>> {
  return request.post<ListResponse<IAvailableCoupon>>('/food/order/orderCouponList', {
    preOrderSn
  });
}

/**
 * 更改支付信息
 */
export function changePayment(payload: {
  preOrderSn: string;
  couponId?: number;
  isBalance?: boolean;
}) {
  return request.post<Response<IPayment>>('/food/order/changePrePayInfo', payload);
}

/**
 * 下单
 */
export function createOrder(payload: {
  preOrderSn: string;
  bookingName: string;
  bookingPhoneArea: string;
  bookingPhone: string;
  bookingMail: string;
  desc: string;
}) {
  return request.post<Response>('/food/order/createOrder', payload);
}

/**
 * 订单列表
 */
export function getOrders(page: number, pageSize: number, orderStatus: string) {
  return request.post<ListResponseWithPagination<IOrderListItem>>('/food/order/orderList', {
    page,
    pageSize,
    orderStatus
  });
}

/**
 * 订单详情
 */
export function getOrder(orderSn: string) {
  return request.post<DetailsResponse<IOrder>>('/food/order/orderDetail', { orderSn });
}

/**
 * 订单核销
 */
export function writeOffOrder(orderSn: string, code: string) {
  return request.post<Response>('/food/order/verify', { orderSn, code });
}

/**
 * 取消订单
 */
export function cancelOrder(orderSn: string) {
  return request.post<Response>('/food/order/cancelOrder', { orderSn, formMember: true });
}
